package 
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	import flash.utils.setTimeout;
	import mylist.FileItem;
	import mylist.IconItem;
	import mylist.logs;
	import mylist.ViewList;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class PhotoData
	{
		private var file_ip:uint;
		private var file_time:uint;
		private var file_size:uint;
		private var isLoaded:Boolean = false;
		private var data:ByteArray;
		
		public function get file_name():String
		{
			if (isLoaded)
				return file_ip + "_" + file_time + ".jpg";
			return file_ip + "_" + file_time;
		}
		
		public function PhotoData(_size:uint, _time:uint, _ip:uint)
		{
			file_size = _size;
			file_ip = _ip;
			file_time = _time;
			
			setTimeout(timeOut, 60000);
		}
		
		private function timeOut():void
		{
			if (data)
				if (file_size == data.length)
				{
					return;
				}
			//file_ip = null;
			//file_time = null;
			//file_size = null;
			//isLoaded = null;
			deleData();
		}
		
		public function get fileSize():uint
		{
			return this.file_size;
		}
		
		public function get ip():uint
		{
			return file_ip;
		}
		
		public function get time():uint
		{
			return file_time;
		}
		
		public function appendData(bytes:ByteArray):Boolean
		{
			if (!data)
			{
				data = new ByteArray();
			}
			data.writeBytes(bytes, bytes.position);
			logs.adds("appendData();");
			logs.adds("photo len:" + data.length, "pos:", data.position);
			if (data.length > file_size)
			{
				logs.adds("file over flow");
				deleData();
				return false;
			}else if(data.length == file_size){
				logs.adds("---------file reve complete-----");
			}
			bytes = null;
			return true;
		}
		
		public function deleData():void
		{
			//logs.adds("deleData()");
			file_size = 0;
			data = null;
		}
		
		public function showData(size:uint, time_:uint, ip_:uint):Boolean
		{
			logs.adds("showData()");
			//if(data)logs.adds("showData()", time_, ip_,data.length,size);
			//logs.adds("data",data);
			if (data && data.length == size)
			{
				isLoaded = true;
				
				//writeToFile(data);
				
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
				loader.loadBytes(data);
				deleData();
				return true;
			}else{//
				logs.adds(""+size + time_ + ip_+"not exist!");
			}
			return false;
		}
		
		private function writeToFile(data:ByteArray):void
		{
			var file:File = new File(File.desktopDirectory.nativePath+"/" + file_name+".jpeg");
			var fileStream:FileStream = new FileStream();
			fileStream.open(file, FileMode.WRITE);
			fileStream.writeBytes(data);
			fileStream.close();
			file = null;
		}
		
		private function loaded(e:Event):void
		{
			ViewList.main.addChilds(e.target.content);
			
			IconItem.showBig(FileItem.list.length-1);
		}
		
		public static var photoDataList:Vector.<PhotoData> = new Vector.<PhotoData>();
		
		public static function getPhotoIndex(_size:uint, _time:uint, _ip:uint):int
		{
			var i:int = isPhotoIn(_size, _time, _ip);
			if (i >= 0)
			{
				return i;
			}
			else
			{
				var item:PhotoData = new PhotoData(_size, _time, _ip);
				photoDataList.push(item);
				return photoDataList.length - 1;
			}
		}
		
		public static function isPhotoIn(_size:uint, _time:uint, _ip:uint):int
		{
			var i:int = photoDataList.length;
			while (i > 0)
			{
				--i;
				if (photoDataList[i].file_time == _time && photoDataList[i].ip == _ip && photoDataList[i].fileSize == _size)
					return i;
			}
			return -1;
		}
	
	}

}
